//package com.ruoyi.personnel.management.department.service;
//
//
//import com.ruoyi.common.utils.StringUtils;
//import com.ruoyi.personnel.management.department.domain.Department;
//import com.ruoyi.personnel.management.department.mapper.DepartmentMapper;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
//import java.util.List;
//
///**
// * 部门管理
// * 业务层处理
// * @author Drj 2021/6/28
// */
//@Service
//public class DepartmentServiceImpl implements IDepartmentService {
//
//    @Autowired
//    private DepartmentMapper departmentMapper;
//
//    /**
//     * 查询部门
//     *
//     * @param department
//     * @return
//     */
//    @Override
//    public List<Department> selectDepartmentList(Department department) {
//        return departmentMapper.selectDepartmentList(department);
//    }
//
//    /**
//     * 根据编号查询部门
//     *
//     * @param departmentId 部门编号
//     * @return
//     */
//    @Override
//    public Department selectDepartmentById(String departmentId) {
//        return departmentMapper.selectDepartmentById(departmentId);
//    }
//
//    /**
//     * 新增部门
//     *
//     * @param department
//     * @return
//     */
//    @Override
//    public int insertDepartment(Department department) {
//        if (StringUtils.isNotNull(departmentMapper.selectDepartmentById(department.getDepartmentId()))) {
//            //已存在
//            return 0;
//        }
//        if (department.getDepartmentParentId() == null || department.getDepartmentParentId() == "") {
//            //无父级产品类别
//            return departmentMapper.insertDepartment(department);
//        }
//        //有父级产品类别
//        Department info = departmentMapper.selectDepartmentById(department.getDepartmentParentId());
//        department.setAncestors(info.getAncestors() + "," + department.getDepartmentParentId());
//        return departmentMapper.insertDepartment(department);
//    }
//
//    /**
//     * 更新部门
//     *
//     * @param department
//     * @return
//     */
//    @Override
//    public int updateDepartment(Department department) {
//        if (!StringUtils.isNotNull(departmentMapper.selectDepartmentById(department.getDepartmentId()))) {
//            //不存在
//            return 0;
//        }
//        //新的父级部门
//        Department newParentDepartment = departmentMapper.selectDepartmentById(department.getDepartmentParentId());
//        //要更新的部门的旧属性
//        Department oldDepartment = departmentMapper.selectDepartmentById(department.getDepartmentId());
//        if (StringUtils.isNotNull(newParentDepartment))//存在新的父级部门
//        {
//            //新的祖级编号
//            String newAncestors = newParentDepartment.getAncestors() + "," + newParentDepartment.getDepartmentId();
//            //旧的祖级编号
//            String oldAncestors = oldDepartment.getAncestors();
//            department.setAncestors(newAncestors);//更新祖级编号
//            updateDepartmentChildren(department.getDepartmentId(), newAncestors, oldAncestors);
//        }
//        return departmentMapper.updateDepartment(department);
//    }
//
//
//
//    /**
//     * 修改子级部门关系
//     * @param departmentId 被修改的部门ID
//     * @param newAncestors 新的父ID集合
//     * @param oldAncestors 旧的父ID集合
//     */
//    public void updateDepartmentChildren(String departmentId, String newAncestors, String oldAncestors)
//    {
//        List<Department> children = departmentMapper.selectChildrenDepartmentById(departmentId);
//        for (Department child : children)
//        {
//            //更新子部门的祖级编号
//            child.setAncestors(child.getAncestors().replace(oldAncestors, newAncestors));
//        }
//        if (children.size() > 0)
//        {
//            departmentMapper.updateDepartmentChildren(children);
//        }
//    }
//
//
//    /**
//     * 删除部门
//     * 仅修改删除标志
//     *
//     * @param department
//     * @return
//     */
//    @Override
//    public int updateDepartmentDeleteFlag(Department department) {
//        return departmentMapper.updateDepartmentDeleteFlag(department);
//    }
//}
